package com.siashan.toolkit.captcha.google;

import com.google.code.kaptcha.GimpyEngine;
import com.google.code.kaptcha.NoiseProducer;
import com.google.code.kaptcha.util.Configurable;
import com.jhlabs.image.RippleFilter;
import com.jhlabs.image.ShadowFilter;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.util.Random;

/**
 * 自定义验证码干扰线生成样式
 * @author siashan
 * @since v1.0.3
 */
public class CustomShadowGimpy extends Configurable implements GimpyEngine {
    public CustomShadowGimpy() {
    }

    @Override
    public BufferedImage getDistortedImage(BufferedImage baseImage) {
        NoiseProducer noiseProducer = this.getConfig().getNoiseImpl();
        BufferedImage distortedImage = new BufferedImage(baseImage.getWidth(), baseImage.getHeight(), 2);
        Graphics2D graph = (Graphics2D)distortedImage.getGraphics();

        // 波浪线设置
        Random rand = new Random();
        RippleFilter rippleFilter = new RippleFilter();
        rippleFilter.setWaveType(0);
        rippleFilter.setXAmplitude(3.6F);
        rippleFilter.setYAmplitude(rand.nextFloat() + 1.0F);
        rippleFilter.setXWavelength((float)(rand.nextInt(8) + 8));
        rippleFilter.setYWavelength((float)(rand.nextInt(6) + 5));
        rippleFilter.setEdgeAction(1);
        BufferedImage effectImage = rippleFilter.filter(baseImage, (BufferedImage)null);
        graph.drawImage(effectImage, 0, 0, (Color)null, (ImageObserver)null);
        graph.dispose();
        // 绘制干扰线 如果想多画几条，就多执行几次.makeNoise()函数
//        noiseProducer.makeNoise(distortedImage, 0.1F, 0.1F, 0.25F, 0.25F);
        noiseProducer.makeNoise(distortedImage, 0.15F, 0.35F, 0.5F, 0.9F);
        return distortedImage;
    }
}
